package com.mdlib.helper.ui.fragments.address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.CityEvnet;
import com.mdlib.helper.model.entity.CityEntity;
import com.mdlib.helper.ui.adapter.CityAdapter;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/12/19 15:52
 * 邮箱：hqy_xz@126.com
 */

public class AreaFragment extends BaseTitleFragment {


    private static final String PID = "pid";

    @Bind(R.id.rv_area)
    RecyclerView mRvArea;
    private int id;

    private CityAdapter mCityAdapter;
    private List<CityEntity> cityList = new ArrayList<>();

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_area;
    }

    public static AreaFragment newInstance(int pid) {

        AreaFragment fragment = new AreaFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(PID, pid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            removeFragment();
        }
        id = getArguments().getInt(PID);


    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.city_select_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    private void init() {
        mCityAdapter = new CityAdapter(cityList);
        mRvArea.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvArea.setAdapter(mCityAdapter);
        mRvArea.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                EventBus.getDefault().post(new CityEvnet(cityList.get(position).getName()));
                getActivity().finish();
            }
        });
        searchCity();
    }

    private void searchCity() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .searchCity(id + "")
                .compose(RxHelper.<ArrayList<CityEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<CityEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<CityEntity> cityEntities) {
                        for (CityEntity city : cityEntities) {
                            cityList.add(city);
                        }
                        mCityAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


}
